package cn.wolfcode.web.Interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 *
 * @author 杨兴健
 * @Date 2020/7/11 22:18
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        获取session
        HttpSession session = request.getSession();
//        获取session里的用户信息
        Object user = session.getAttribute("USER_IN_SESSION");
//        判断用户是否为空
        if (user == null) {
//            用户为空,则跳转到登录页面
            response.sendRedirect("/index.html");
            return false;
        }
//        用户不为空,则放行
        return true;
    }
}
